/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/crypto/cryptoAES.cpp,v 1.4 2003/11/21 12:39:24 itsme Exp $
 *
 *
 *  wrapper class around AES implementation in machine\spcore\crypto.c
 *
 *  used by random number generator
 *
 *  naming of this class is not that good!
 *  also note that the corresponding header file is named differently.
 *
 *  I should subclass the BlockCyper.
 *
 *  code to verify the correct functioning of this module is in cryptoVerifier.cpp
 *
 */
#include "fnaBlockCypher.h"

#include "stringutils.h"
#include "debug.h"

#include "crypto.h" // from the spcore directory


BlockCypher::BlockCypher()
{
    ClearKey();
}

BlockCypher::~BlockCypher()
{
    ClearKey();
}


void BlockCypher::ClearKey()
{
    ByteVector key(8*4);
    SetKey(key);
}

bool BlockCypher::SetKey(const ByteVector& key)
{
    if (key.size()!=BlockCypher::KEYSIZE)
        return false;

    m_key= key;

    crypto_aes_setup(m_state, (DWORD*)vectorptr(key));

    return true;
}

const ByteVector& BlockCypher::GetKey() const
{
    return m_key;
}

ByteVector BlockCypher::Encrypt(const ByteVector& plain)
{
    ByteVector result(BlockCypher::BLOCKSIZE);
    crypto_aes_encrypt(m_state, (const DWORD*)vectorptr(plain), (DWORD*)vectorptr(result));
    return result;
}

